<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="./styles/index.css" />
  <title>Document</title>
</head>

<body>
  <div id="app" class="score-case">
    <div class="table">
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>科目</th>
            <th>成绩</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody v-if="list.length>0">
          <!-- 1.渲染页面 -->
          <tr v-for="(e,i) in list" :key="e.id">
            <td>{{i+1}}</td>
            <td>{{e.subject}}</td>
            <!-- 3.red类名应该为动态 判断成绩是否小于60 -->
            <td :class="{red:e.score<60}">{{e.score}}</td>
            <!-- 4.删除设置点击事件 -->
            <td><a href="#" @click="re(e.id)">删除</a></td>
          </tr>
        </tbody>
        <!-- 2.暂无数据默认隐藏 使用 v-if v-else -->
        <tbody v-else>
          <tr>
            <td colspan="5">
              <span class="none">暂无数据</span>
            </td>
          </tr>
        </tbody>

        <tfoot>
          <tr>
            <td colspan="5">
              <span>总分：{{str}}</span>
              <span style="margin-left: 50px">平均分：{{str2}}</span>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
    <div class="form">
      <div class="form-item">
        <div class="label">科目：</div>
        <div class="input">
          <!-- 双向绑定 获取值 -->
          <input type="text" placeholder="请输入科目" v-model="subject" />
        </div>
      </div>
      <div class="form-item">
        <div class="label">分数：</div>
        <div class="input">
          <!-- 双向绑定 获取值 -->
          <input type="text" placeholder="请输入分数" v-model="score" />
        </div>
      </div>
      <div class="form-item">
        <div class="label"></div>
        <div class="input">
          <!-- 5.添加点击事件 -->
          <button class="submit" @click="add">添加</button>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

  <script>
    const app = new Vue({
      el: '#app',
      data: {
        list: [
          { id: 1, subject: '语文', score: 20 },
          { id: 7, subject: '数学', score: 99 },
          { id: 12, subject: '英语', score: 70 },
        ],
        subject: '',
        score: ''
      },
      methods: {
        // 删除功能实现
        re(id) {
          this.list = this.list.filter(e => e.id != id)
        },
        add() {
          // 非空判断
          if (this.subject === '' || this.score === '') {
            alert('内容不能为空！')
            return
          }
          // 将内容添加到新数组
          this.list.push({ id: Math.random(), subject: this.subject, score: this.score })
          // 表单滞空
          this.subject = ''
          this.score = ''
        }
      },
      computed: {
        // 6.求底部总和及平均数
        // 总和
        str() {
          return this.list.reduce((prev, e) => prev + e.score,0)
        },
        // 平均分
        str2() {
          // 逻辑中断判断 如果没有成绩 则为0
          return this.str / this.list.length  ||  0
        }
      }
    })
  </script>
</body>

</html>